package com.sita.redsun.other.test;

import com.google.common.io.BaseEncoding;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;

/**
 * Created by zhebinwang on 2017/3/21.
 */
public class ClientMainV2 {


    private int sendnumber=0; //到底发送了多少次数据。
    private int threadnumber=-1;//线程号

    public int getSendnumber() {
        return sendnumber;
    }

    public int getThreadnumber() {
        return threadnumber;
    }

    public void setThreadnumber(int threadnumber) {
        this.threadnumber = threadnumber;
    }

    public void setSendnumber(int sendnumber) {
        this.sendnumber = sendnumber;
    }

    public  void dotest(String tid, String ip, String port) throws Exception {
        ClientMainV2 clictGpsList = new ClientMainV2();
        Socket socket = new Socket();
        //Socket socket = new Socket("localhost",6800).setKeepAlive(false);
        socket.setTcpNoDelay(true);
        socket.setKeepAlive(true);
        //123.206.201.243
        //InetSocketAddress i = new InetSocketAddress("123.206.201.243", 6800); //春风测试
        //InetSocketAddress i = new InetSocketAddress("192.168.3.42", 6200); //雅迪生产1
        //InetSocketAddress i = new InetSocketAddress("127.0.0.1", 6200); //雅迪生产1
        //InetSocketAddress i = new InetSocketAddress("115.159.160.16", 6200); //雅迪生产1
        InetSocketAddress i=new InetSocketAddress(ip,Integer.valueOf(port));
        //InetSocketAddress i = new InetSocketAddress("115.159.125.20", 6200); //雅迪生产2
        //115.159.146.78
        //InetSocketAddress i = new InetSocketAddress("115.159.146.78", 6200); //雅迪备用链接
        //InetSocketAddress i = new InetSocketAddress("120.27.217.246", 6800); //春风生产


        //InetSocketAddress i = new InetSocketAddress("182.254.146.197", 6200); //雅迪生产1ol
        //182.254.146.197
        socket.connect(i);

        OutputStream out = (socket.getOutputStream());




        //开启接收线程
        ReciveThread reciveThread=new ReciveThread();
        reciveThread.setSocket(socket);
        reciveThread.setLogflag(1);
        reciveThread.start();


        System.out.println("begin sEND"+new Date().toString());



        //0866686023764011
        byte[] data1 = null;
        String [] msgs=new String[4];
        msgs[0]="78782D19"+tid+"04011104277B440C99D6EB0000000000000300FF000000640000006400000A040901CC490D0A";
        msgs[1]="78782D19"+tid+"04011104277D060C99E99A0000000000000300FF000000640000006400000A040901CC490D0A";
        msgs[2]="78782D19"+tid+"04011104276B600C99ECB20000000000000300FF000000640000006400000A040901CC490D0A";
        msgs[3]="78782D19"+tid+"0401110427686C0C99D3CE0000000000000300FF000000640000006400000A040901CC490D0A";




        for (int x=0;x<16;x++) {
            //一个四方形的圈  循环运行5次
            for (int y=0;y<4;y++) {
                data1 = BaseEncoding.base16().decode(msgs[y]);
                try {
                    out.write(data1);
                    sendnumber++;
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        Thread.sleep(3000);

        //最后一条消息的骑行状态为1
        String endmsg="78782D19"+tid+"0401110427686C0C99D3CE0000000000000100FF000000640000006400000A040901CC490D0A";
        data1 = BaseEncoding.base16().decode(endmsg);
        try {
            out.write(data1);
            sendnumber++;
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Cli19.threadRunStructs[threadnumber].setFlag(true);
        Cli19.threadRunStructs[threadnumber].setSendnumber(sendnumber);


        //此处删除了循环登录包的数据

        //检查是否所有的线程都执行完成了
        boolean allover=true;
        for (int x=0;x<Cli19.threadRunStructs.length;x++){
            if (!Cli19.threadRunStructs[x].isFlag()){
                allover=false;
                break;
            }
        }
        if (allover){
            Cli19.allThreadIsOver=true;
        }


    }




    public static void main(String[] args) throws Exception {
        // readFileZip();
        ClientMainV2 clientMainV2=new ClientMainV2();
        clientMainV2.dotest("0861358030204044","127.0.0.1","6200");
    }




    //形成文件

    public static void  readFileZip(){
        try {
            String jsonStr =  textByDealerFilePath("/Users/clp/rountpoint/routes556892329869314213.json");

            JSONObject json_test = JSONObject.fromObject(jsonStr);

            // System.out.print(json_test);

            System.out.println(json_test.get("points"));

            System.out.println(json_test);

            JSONArray backBodyJson = (JSONArray) json_test.get("points");


            //String StrData="[{\"name\":\"zhangsan\"},{\"age\":\"18\"}]";
            //JSONArray backBodyJson = JSONArray.fromObject(StrData ); // 首先把字符串转成 JSONArray 对象
            if(backBodyJson.size()>0){
                for(int i=0;i<backBodyJson.size();i++) {
                    JSONObject job = backBodyJson.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                    System.out.println(job.get("latitude"));
                    System.out.println(job.get("longitude")); // 得到 每个对象中的属性值

                }}





        }catch(IOException e){
            e.printStackTrace();
        }

    }


    public static String textByDealerFilePath(String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        byte[] byts = new byte[fis.available()];
        fis.read(byts);
        fis.close();
        return new String(byts, "utf-8");
    }



}
